﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALFirmAdress : DALBase
    {
        public static DALFirmAdress Singleton = new DALFirmAdress();

        public GTFirmAdress Add(GTFirmAdress gtFirmAdress)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newFirm = new FirmAdress();

                newFirm.Adress = gtFirmAdress.Adress;
                newFirm.Coment = gtFirmAdress.Coment;
                newFirm.FirmId = gtFirmAdress.FirmId;
                newFirm.GoogleMap = gtFirmAdress.GoogleMap;
                newFirm.Phone = gtFirmAdress.Phone;
                newFirm.Skype = gtFirmAdress.Skype;
                newFirm.WorkingTime = gtFirmAdress.WorkingTime;
             
                db.FirmAdresses.InsertOnSubmit(newFirm);
                db.SubmitChanges();

                gtFirmAdress.Id = newFirm.Id;
                return gtFirmAdress;
            }
        }


        public GTFirmAdress Update(GTFirmAdress gtFirmAdress)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var newFirm = db.FirmAdresses.SingleOrDefault(x => x.Id == gtFirmAdress.Id);

                newFirm.Adress = gtFirmAdress.Adress;
                newFirm.Coment = gtFirmAdress.Coment;
                newFirm.FirmId = gtFirmAdress.FirmId;
                newFirm.GoogleMap = gtFirmAdress.GoogleMap;
                newFirm.Phone = gtFirmAdress.Phone;
                newFirm.Skype = gtFirmAdress.Skype;
                newFirm.WorkingTime = gtFirmAdress.WorkingTime;

                db.SubmitChanges();
                return gtFirmAdress;
            }
        }


        public GTFirmAdress Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.FirmAdresses.SingleOrDefault(x => x.Id == id);
                GTFirmAdress gtBrand = new GTFirmAdress();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTFirmAdress> GetPaged(int firmId,int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.FirmAdresses
                 where p.FirmId == firmId
                 orderby p.Id
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTFirmAdress>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTFirmAdress gtBrand = new GTFirmAdress();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }



        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.FirmAdresses.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.FirmAdresses.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

    }
}
